package com.spider.silence.downloader;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.silence.abstarct.downloader.AbstractLoginDownLoader;
import com.spider.silence.main.ChengduLifeMain;
import com.spider.silence.proxy.ProxyPool;
import com.spider.silence.task.ChengduLifeTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.*;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import us.codecraft.webmagic.Page;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * Created by qiuhao on 2017/1/11.
 */
public class ChengduLifeDownLoader extends AbstractLoginDownLoader implements Cloneable {
    private Logger logger = Logger.getLogger(ChengduLifeDownLoader.class);
    private String url = "http://www.028sh.com/forum.php";
    private String basicUrl = "http://www.028sh.com/";
    private String username="silence"; //用户名
    private String password="silence";//密码
    private Map<String,File> files=null;

    public Map<String,File> getFiles(){

        return files;
    }

    public ChengduLifeDownLoader() {

    }

//    /**
//     * 移动鼠标到某个页面元素上
//     * @param url
//     * @param element 鼠标要移动到的元素
//     */
//    public void simulateMouseMove(String url,String element){
//        WebDriver driver=new ChromeDriver();
//        driver.get(url);
//        driver.manage().window().maximize();
//        Actions builder = new Actions(driver);
//        WebElement element1=driver.findElement(By.cssSelector(element));
//        Action mouserOverlogin = builder.moveToElement(element1).build();
//        mouserOverlogin.perform();
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }finally {
//            driver.close();
//        }
//
//    }

    //模拟登陆
    @Override
    protected HashSet<Cookie> login() {
        ChromeDriver driver = null;
        try {
            driver = ChromeDriverUtils.getDriver();
//            driver.manage().window().maximize();
            driver.get(basicUrl);
            WebDriverWait loginFormWait = new WebDriverWait(driver, 20000);
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(),e);
                    e.printStackTrace();
                }
                if (driver.findElement(By.cssSelector("#ls_username")).isEnabled()) {
                    loginFormWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#ls_username")))
                            .sendKeys(username);
                    break;
                }
            }
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(),e);
                    e.printStackTrace();
                }
                if (driver.findElement(By.cssSelector("#ls_password")).isEnabled()) {
                    loginFormWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#ls_password")))
                            .sendKeys(password);
                    break;
                }
            }
            loginFormWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".pn.vm.comiis_dlan"))).click();
            Thread.sleep(2000);
            return (HashSet<Cookie>) driver.manage().getCookies();

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        } finally {
            driver.close();
            driver.quit();
        }

        return null;
    }

    /**
     * @return List<String>  返回每个模块的URL集合
     */
    public List<String> getModulesUrlList() {
        List<String> urlList = new ArrayList<String>();
        Document document=null;
        try {
            Page pageContent = null;
            pageContent = getHtml(url);
            if (null == pageContent) {

            } else if(pageContent.getStatusCode()==200) {
                document = get(url);
                Element element = document.body();
                Elements module = element.select(".mn").select(".bm_c").select(".fl_g");
                for (int i = 0; i < module.size(); i++) {
                    String moduleUrl = "http://www.028sh.com/forum.php?mod=forumdisplay&fid=" +
                            module.get(i).select("dt").select("a").attr("href").split("-")[1] + "&filter=sortall&sortall=1";
                    urlList.add(moduleUrl);
                }
            }
        } catch (Exception e) {
            Logger logger = Logger.getLogger(ChengduLifeMain.class);
            logger.error("获取成都生活论坛版块url出错",e);
        }
        return urlList;
    }

    /**
     * 生成task 添加到TaskManager中（获取每个模块中第一页的帖子的url）
     *
     * @param urlList
     */
    public void generateTask(List<String> urlList) {
        for (int i = 0; i < urlList.size(); i++) {
            Document document=null;
            try {
                Page pageContent =  getHtml(urlList.get(i));
                if(pageContent.getStatusCode()==200) {
                    document = pageContent.getHtml().getDocument();
                    Elements element = document.select(".s.xst");
                    for (int j = 0; j < element.size(); j++) {
                        String url = element.get(j).attr("href");
                        ChengduLifeTask task = new ChengduLifeTask(URLDecoder.decode(url, "UTF-8"));
                        CommonTaskManager.add("ChengDuLife", task);

                    }
                    Elements element2 = document.select(".ml.waterfall.cl").select(".c.cl");
                    for (int k = 0; k < element2.size(); k++) {
                        String url = element2.get(k).select(".z").attr("href");
                        ChengduLifeTask task = new ChengduLifeTask(URLDecoder.decode(url, "UTF-8"));
                        CommonTaskManager.add("ChengDuLife", task);

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Logger logger = Logger.getLogger(ChengduLifeMain.class);
                logger.error("获取成都生活论坛版块中帖子的url出错",e);
            }
        }
    }

    /**
     *  获取历史帖子的url
     * @param urlList
     */
    public void generateAllTask(List<String> urlList){
        for (int i = 0; i < urlList.size(); i++) {
            Document document=null;
            try{
                Page pageContent = getHtml(urlList.get(i));
                if (null == pageContent) {

                } else if(pageContent.getStatusCode()==200) {
                    document  = pageContent.getHtml().getDocument();
                    int page = 0;
                    if (document.select("#fd_page_top").select(".pg").size() == 0) {
                        page = 1;
                    } else {
                        page = Integer.parseInt(document.select("#fd_page_top").select(".pg").select("label").select("span").get(0).attr("title").split(" ")[1]); //页码
                    }

                    for (int j = 1; j < page + 1; j++) {
                        try {
                            System.out.println(urlList.get(i) + "&page=" + j);
                            Document d = get(urlList.get(i) + "&page=" + j);
                            Elements element = d.select(".s.xst");
                            if (element.size() > 0) {
                                for (int k = 0; k < element.size(); k++) {
                                    String url = element.get(k).attr("href");
                                    ChengduLifeTask task = new ChengduLifeTask(URLDecoder.decode(url, "UTF-8"));
                                    CommonTaskManager.add("ChengDuLife", task);
                                }
                            }
                            Elements element2 = d.select(".ml.waterfall.cl").select(".c.cl");
                            if (element2.size() > 0) {
                                for (int k = 0; k < element2.size(); k++) {
                                    String url = element2.get(k).select(".z").attr("href");
                                    ChengduLifeTask task = new ChengduLifeTask(URLDecoder.decode(url, "UTF-8"));
                                    CommonTaskManager.add("ChengDuLife", task);
                                }
                            }

                        } catch (Exception e) {
                            continue;
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                Logger logger = Logger.getLogger(ChengduLifeMain.class);
                logger.error("获取成都生活论坛版块中帖子的url出错",e);
            }
        }
    }


    /**
     * @param url 帖子的url
     * @return 每个帖子的内容
     */
    public JSONObject crawlItem(String url) {
        files=new ConcurrentHashMap<>();
        JSONObject item = new JSONObject();
        JSONObject data = new JSONObject();
        int page = 1;
        Document document=null;
        try {
            boolean flag = true;
            long startTime = System.currentTimeMillis();
            long nowTime = startTime;
            Map<String,File> speechMap=new ConcurrentHashMap<>(); //存放帖子内容的map
            if(!"".equals(url) && url!=null) {
                while (flag && nowTime - startTime < Long.valueOf(PropertiesUtil.getVal("ChengDuLife.TimeOut"))) {
                    try{
                        Page pageContent = getHtml(url);

                        if (pageContent == null) {

                        } else if (pageContent.getStatusCode() == 200) {
                            try {
                                document = pageContent.getHtml().getDocument();
                                document.select(".aimg_tip").remove();
                                String title = document.title().split("-")[0]; //帖子标题
                                Element publishContent = document.select(".pl.bm.thfd.comiis_x0").get(0);
                                if (publishContent.select(".pls").get(1).select(".pi").select(".authi").size() != 0) {
                                    int read_count = Integer.parseInt(publishContent.select(".hm.ptn").select("span").get(1).text()); //帖子查看次数
                                    int reply_count = Integer.parseInt(publishContent.select(".hm.ptn").select("span").get(4).text());//帖子回复次数
                                    data.put("title", title);
                                    data.put("read_count", read_count);
                                    data.put("reply_count", reply_count);
                                    data.put("url", url);
                                    page = (int) Math.ceil(((double) reply_count + 1) / 10d);//页数

                                    Element publichTimeElement = publishContent.select(".comiis_vrx").get(0).select(".pti").select(".authi").get(0);
                                    String publichTime = "";
                                    if (publichTimeElement.select("em").select("span").hasAttr("title")) {
                                        publichTime = publichTimeElement.select("em").select("span").attr("title");
                                    } else {
                                        publichTime = publichTimeElement.select("em").text().split(" ")[1] + " " + publichTimeElement.select("em").text().split(" ")[2];  //帖子发表时间
                                    }
                                    data.put("publish_time", publichTime);
                                    Date now = new Date();
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    data.put("spider_time", sdf.format(now)); //爬取时间
                                    String content = publishContent.select(".t_fsz").select(".t_f").removeAttr("iframe").html();//剔除发布内容中的广
                                    JSONArray img_urls=new JSONArray();
                                    Elements imgElements=publishContent.select(".t_fsz").select(".t_f").removeAttr("iframe").select("img[zoomfile]");
                                    for(int imgIndex=0;imgIndex<imgElements.size();imgIndex++){
                                        Element imgElement=imgElements.get(imgIndex);
                                        String img_url=imgElement.absUrl("zoomfile");
                                        img_urls.add(img_url);
                                    }
                                    data.put("img_urls",img_urls);
                                    data.put("content", FileUtils.filterImg(content)); //发布内容
                                    String collect = publishContent.select(".mtw.mbm.hm.cl").select("a").get(0).select("span").text();
                                    if (!"".equals(collect) && collect != null) {
                                        int collect_count = Integer.parseInt(publishContent.select(".mtw.mbm.hm.cl").select("a").get(0).select("span").text());//收藏数
                                        data.put("collect_count", collect_count);
                                    }
                                    String publish_user = publishContent.select(".pls").get(1).select(".authi").select("a").text();//用户名
                                    data.put("publish_user", publish_user);
                                    String publish_user_id = "";
                                    String publish_user_photo = "";
                                    if (publishContent.select(".pls").get(1).select(".authi").select("a").hasAttr("target")) {
                                        publish_user_id = publishContent.select(".pls").get(1).select(".authi").select("a").attr("href").split("-")[2].split("\\.")[0];
                                        publish_user_photo = "http://www.028sh.com/uc_server/avatar.php?uid=" + publish_user_id + "&size=middle";
                                    } else {
                                        publish_user_photo = "http://www.028sh.com/source/plugin/iwenwen/template/avatar.jpg";
                                    }
                                    data.put("publish_user_id", publish_user_id);
                                    data.put("publish_user_photo", publish_user_photo);
                                    String id = url.split("&")[1].split("=")[1];//帖子id
                                    data.put("id", id);

                                    long tsp= DateUtils.dateToTimestamp( DateUtils.parseDate(publichTime)).getTime();
                                    String filePrefix="ChengDuLife";//文件前缀
                                    String fileName="";
                                    if(publish_user_id.equals("") || null==publish_user_id){
                                        fileName=filePrefix+"_"+tsp+"_"+id;
                                    }else{
                                        fileName=filePrefix+"_"+tsp+"_"+publish_user_id+"_"+id;
                                    }

                                    String publishDate=DateUtils.formatDate(DateUtils.parseDate(publichTime)).replace("-","-");
                                    String speeceId="ChengDuLife_speeches_"+publishDate+"-"+fileName;
                                    File file= FileUtils.createTempFile(speeceId+"_pageNo_1",document.html().toString());
                                    files.put(speeceId+"_pageNo_1",file);
                                    //帖子首页的文件

                                    //将本页面的回复添加上去
                                    int size = document.select(".comiis_vrx").size();
                                    Elements replyEntities = document.select(".comiis_vrx:gt(1):lt(" + size + ")");
                                    JSONArray reply_nodes = new JSONArray();
                                    for (int i = 0; i < replyEntities.size(); i++) {
                                        try {
                                            JSONObject reply = new JSONObject();
                                            Element replyUserInfo = replyEntities.get(i).select(".pls").get(0);
                                            String replyUser = replyUserInfo.select(".authi").select("a").get(0).text();
                                            String homeUrl = replyUserInfo.select(".authi").select("a").get(0).attr("href");
                                            String replyUserId = "";
                                            String replyUserPhoto = "";
                                            if (replyUserInfo.select(".authi").select("a").hasAttr("target")) {
                                                replyUserId = homeUrl.split("-")[2].split("\\.")[0];
                                                replyUserPhoto = "http://www.028sh.com/uc_server/avatar.php?uid=" + replyUserId + "&size=middle";
                                            } else {
                                                replyUserPhoto = "http://www.028sh.com/source/plugin/iwenwen/template/avatar.jpg";
                                            }
                                            String replyContent = "";
                                            JSONArray reply_img_urls=new JSONArray();
                                            Elements replyImgElements=null;
                                            if (replyEntities.get(i).select(".t_fsz").size() > 0) {
                                                replyContent = replyEntities.get(i).select(".t_fsz").html();
                                                replyImgElements=replyEntities.get(i).select(".t_fsz").select("img[zoomfile]");
                                            } else {
                                                replyContent = replyEntities.get(i).select(".pct").html();
                                                replyImgElements=replyEntities.get(i).select(".pct").select("img[zoomfile]");
                                            }
                                            if(replyImgElements.size()>0){
                                                for(int imgIndex=0;imgIndex<replyImgElements.size();imgIndex++){
                                                    Element imgElement=replyImgElements.get(imgIndex);
                                                    String img_url=imgElement.absUrl("zoomfile");
                                                    reply_img_urls.add(img_url);
                                                }
                                            }


                                            String replyTime = "";
                                            if (replyEntities.get(i).select(".authi").select("em").select("span").hasAttr("title")) {
                                                replyTime = replyEntities.get(i).select(".authi").select("em").select("span").attr("title");
                                            } else {
                                                replyTime = getTime(replyEntities.get(i).select(".authi").html());
                                            }

                                            int like_count = 0;
                                            int dislike_count = 0;
                                            if (!"".equals(replyEntities.get(i).select(".pob.cl").select("em").select("a").get(2).select("scan").text())) {
                                                like_count = Integer.parseInt(replyEntities.get(i).select(".pob.cl").select("em").select("a").get(2).select("scan").text());
                                            }
                                            if (!"".equals(replyEntities.get(i).select(".pob.cl").select("em").select("a").get(3).select("scan").text())) {
                                                dislike_count = Integer.parseInt(replyEntities.get(i).select(".pob.cl").select("em").select("a").get(3).select("scan").text());
                                            }

                                            String replyUrl = url + "#" + replyEntities.get(i).id();
                                            reply.put("publish_user", replyUser);
                                            reply.put("publish_user_id", replyUserId);
                                            reply.put("publish_user_photo", replyUserPhoto);
                                            reply.put("content", FileUtils.filterImg(replyContent));
                                            reply.put("img_urls",reply_img_urls);
                                            reply.put("publish_time", replyTime);
                                            reply.put("url", replyUrl);
                                            reply.put("homeUrl", homeUrl);
                                            reply.put("parent_id", id);
                                            reply.put("ancestor_id", id);
                                            reply.put("like_count", like_count);
                                            reply.put("dislike_count", dislike_count);
                                            reply_nodes.add(reply);
                                        }catch (Exception e){
                                            logger.error(e.getMessage(),e);
                                            continue;
                                        }
                                    }
                                    JSONArray replys = new JSONArray();
                                    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                                    if (page >= 2) {
                                        int threadNum = Integer.valueOf(PropertiesUtil.getVal("ChengDuLife.ReplyThreadCount"));
                                        List<FutureTask<JSONArray>> futureTasks = new ArrayList<FutureTask<JSONArray>>();
                                        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
                                        try {
                                            ArrayList<ArrayList<Integer>> splitPages = CommonUtils.splitPages(2, page, threadNum);
                                            for (int i = 0; i < splitPages.size(); i++) {
                                                ChengduLifeDownLoader downLoader = this.cloneDownLoader();
                                                ReplyCrawler callable = new ReplyCrawler(downLoader, splitPages.get(i), url, id,speeceId);
                                                FutureTask<JSONArray> futureTask = new FutureTask<JSONArray>(callable);
                                                futureTasks.add(futureTask);
                                                executorService.submit(futureTask);
                                            }
                                            int count = 0;
                                            for (FutureTask<JSONArray> futureTask : futureTasks) {
                                                JSONArray replyArrays = futureTask.get();
                                                for (int i = 0; i < replyArrays.size(); i++) {
                                                    JSONObject object = replyArrays.getJSONObject(i);
                                                    replys.add(object);
                                                }
                                            }
                                            if (replys.size() > 0 && replys != null) {
                                                for (int i = 0; i < replys.size(); i++) {
                                                    JSONObject reply_object = replys.getJSONObject(i);
                                                    reply_nodes.add(reply_object);
                                                }
                                            }
                                            if (reply_nodes.size() > 0 && reply_nodes != null) {
                                                for (int i = 0; i < reply_nodes.size(); i++) {
                                                    JSONObject reply_object = reply_nodes.getJSONObject(i);
                                                    Map<String, Object> map = reply_object;
                                                    list.add(map);
                                                }
                                            }
                                            Collections.sort(list, new Comparator<Map<String, Object>>() {

                                                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                                                    String s1 = o1.get("publish_time").toString();
                                                    String s2 = o2.get("publish_time").toString();
                                                    int flag = s1.compareTo(s2);
                                                    return flag;
                                                }
                                            });
                                        }catch (Exception e){
                                            logger.error(e.getMessage(),e);
                                            e.printStackTrace();
                                        }finally {
                                            executorService.shutdown();
                                        }
                                    } else {
                                        if (reply_nodes.size() > 0 && reply_nodes != null) {
                                            for (int i = 0; i < reply_nodes.size(); i++) {
                                                JSONObject reply_object = reply_nodes.getJSONObject(i);
                                                Map<String, Object> map = reply_object;
                                                list.add(map);
                                            }
                                        }

                                    }
                                    data.put("reply_nodes", list);
                                }
                            } catch (Exception e) {
                                logger.error(e.getMessage(),e);
                            }finally{
                                flag = false;
                            }
                        }
                    }catch (Exception e){
                        logger.error(e.getMessage(),e);
                    }finally{
                        if(flag==true){
                            item = new JSONObject();
                            data = new JSONObject();
                        }
                        nowTime = System.currentTimeMillis();
                    }
                }
            }
        } catch (Exception e) {
            item = new JSONObject();
            data = new JSONObject();
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }
        item.put("data",data);
        return item;
    }

    public class ReplyCrawler implements Callable<JSONArray> {
        private ChengduLifeDownLoader downLoader;
        private ArrayList<Integer> pages;
        private String url;
        private String parent_id;
        private String speech_id;


        @Override
        public JSONArray call() throws Exception {
            JSONArray array = new JSONArray();
            for (int k = 0; k < pages.size(); k++) {
                JSONArray reply_nodes = new JSONArray();
                Document document=null;
                try {
                    String getUrl = url + "&page=" + pages.get(k);
                    Page pageContent = downLoader.getHtml(url);

                    if (pageContent == null) {

                    } else if (pageContent.getStatusCode() == 200) {
                        document = pageContent.getHtml().getDocument();
                        document.select(".aimg_tip").remove();
                        File file=FileUtils.createTempFile(speech_id+"-pageNo-"+pages.get(k),document.html().toString());
                        files.put(speech_id+"-pageNo-"+pages.get(k),file);
                        Elements replyEntities = document.select(".rtv");
                        for (int i = 0; i < replyEntities.size(); i++) {
                            try {
                                Element replyUserInfo = replyEntities.get(i).select(".pls").get(0);
                                JSONObject reply = new JSONObject();
                                String replyUser = replyUserInfo.select(".authi").select("a").get(0).text();
                                String homeUrl = replyUserInfo.select(".authi").select("a").get(0).attr("href");
                                String replyUserId = "";
                                String replyUserPhoto = "";
                                if (replyUserInfo.select(".authi").select("a").get(0).hasAttr("target")) {
                                    replyUserId = getUrlParams(homeUrl).get("uid").toString();
                                    replyUserPhoto = "http://www.028sh.com/uc_server/avatar.php?uid=" + replyUserId + "&size=middle";
                                } else {
                                    replyUserPhoto = "http://www.028sh.com/source/plugin/iwenwen/template/avatar.jpg";
                                }
                                String replyContent = "";
                                JSONArray reply_img_urls=new JSONArray();
                                Elements replyImgElements=null;
                                if (replyEntities.get(i).select(".t_fsz").size() > 0) {
                                    replyContent = replyEntities.get(i).select(".t_fsz").html();
                                    replyImgElements=replyEntities.get(i).select(".t_fsz").select("img[zoomfile]");
                                } else {
                                    replyContent = replyEntities.get(i).select(".pct").html();
                                    replyImgElements=replyEntities.get(i).select(".pct").select("img[zoomfile]");
                                }
                                if(replyImgElements.size()>0){
                                    for(int imgIndex=0;imgIndex<replyImgElements.size();imgIndex++){
                                        Element imgElement=replyImgElements.get(imgIndex);
                                        String img_url=imgElement.absUrl("zoomfile");
                                        reply_img_urls.add(img_url);
                                    }
                                }
                                String replyTime = "";
                                if (replyEntities.get(i).select(".authi").select("em").select("span").hasAttr("title")) {
                                    replyTime = replyEntities.get(i).select(".authi").select("em").select("span").attr("title");
                                } else {
                                    replyTime = getTime(replyEntities.get(i).select(".authi").html());
                                }
                                String replyUrl = getUrl + "#" + replyEntities.get(i).id();

                                reply.put("publish_user", (replyUser));
                                reply.put("publish_user_id", replyUserId);
                                reply.put("publish_user_photo", replyUserPhoto);
                                reply.put("content", FileUtils.filterImg(replyContent));
                                reply.put("img_urls",reply_img_urls);
                                reply.put("publish_time", replyTime);
                                reply.put("url", replyUrl);
                                reply.put("homeUrl", homeUrl);
                                reply.put("parent_id", parent_id);
                                reply.put("ancestor_id", parent_id);
                                reply_nodes.add(reply);

                            } catch (Exception e) {
                                logger.error(e.getMessage(),e);
                                continue;//因为禁言无法获取的情况
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.error(e);
                    e.printStackTrace();
                    continue;
                }finally {
                    array.addAll(reply_nodes);
                }
            }
            return array;


        }

        public ReplyCrawler(ChengduLifeDownLoader downLoader, ArrayList<Integer> pages, String url, String parent_id,String speech_id) {
            this.downLoader = downLoader;
            this.pages = pages;
            this.url = url;
            this.parent_id = parent_id;
            this.speech_id=speech_id;
        }

    }
    public ChengduLifeDownLoader cloneDownLoader() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        ChengduLifeDownLoader loader = new ChengduLifeDownLoader();
        loader.cookies = (HashSet<Cookie>) this.cookies.clone();
        loader.headers = (HashMap<String, String>) this.headers.clone();
        return loader;
    }
}
